import { combineReducers } from 'redux'
import { routerReducer as routing } from 'react-router-redux'
import {
  INCREMENT,DECREMENT,TOCHANGETITLE,LOGIN,APPLYSTATUS,PRODUCTEXPIRE,
  STORE,GROUP,INFOLIST,GROUPDETAIL,STOREDETAIL,STOREALREADYOPEN,
  GROUPALREADYOPEN,BIZMSGCOUNT,SYSMSGCOUNT,STORECOUNT,GROUPCOUNT,
  APPLYORDERLIST,APPLYORDERDETAIL,APPLYORDERTRACK,USERINFO,SERVICERINFO,
  AREA,PROVINCE,MODIFYSERVICER,REGION,CITY,PRODUCT,TENANTPUSH,ADDSHOPCARTOTAL,SUBSHOPCARTOTAL,SUBALLSHOPCARTOTAL,
    WXMTSETSTOREINFO
} from '../actions'
import hhistory from '../history'
import wxSmartdrs from './smartDr.js'
import wxElms from './elm.js'

const changeCount = ( state = 0 , action ) => {
	switch (action.type) {
	    case INCREMENT:
	      return state + 1
	    case DECREMENT:
	      return state - 1
	    default:
	      return state
	}
}

const changeNav = ( state = {title:"快捷登陆"},action ) => {
	if( action.type === TOCHANGETITLE ){
		return action.navobj
	}else{
		return state
	}
}

const loginStatus = ( state = { status:"nologin" },action ) => {
	if( action.type === LOGIN ){
		sessionStorage.setItem( "loginstatus", JSON.stringify( action.data.data ) )
		return action.data.data
	}else if( sessionStorage.getItem( "loginstatus" ) ){
		return JSON.parse( sessionStorage.getItem( "loginstatus" ) )
	}else{
		return state
	}
}

const changeShopCarTotal = (state = {price:0,active:0,commodityInfo :[]},action) => {
	if( action.type === ADDSHOPCARTOTAL) {
        state.price = state.price + action.json.price
        state.active = state.active + action.json.active
        state.commodityInfo.push( action.json.commodityInfo)
        return state
	}else if( action.type === SUBSHOPCARTOTAL) {
        console.log(state.price,action.json.price)
        state.price = state.price - action.json.price
        state.active = state.active - action.json.active
        state.commodityInfo = state.commodityInfo.filter((v) => {
            return v.shoppingItemId !== action.json.commodityInfo.shoppingItemId
        })
        return state
	}else if(action.type === SUBALLSHOPCARTOTAL){
	    return (state = {price:0,active:0,commodityInfo :[]})
    }else{
		return state
	}
}


const tenantPushStatus = ( state = {},action ) => {
    console.log(action);
    if( action.type === TENANTPUSH ){

        return action.data.data
    }else{
        return state
    }
}

const applyStatus = ( state={ awaitPayMentNum:1 },action ) =>{
	if( action.type === APPLYSTATUS ){
		state = action.data.data
	}
	return state
}

const productExpire = ( state = [],action ) => {
	if( action.type === PRODUCTEXPIRE ){
		state = action.data.data
	}
	return state
}

const groupList = ( state = {detail:[],
							total:0,
							totalNum:0,
							gttotal:0,
							tomonthexpire:0,
							tomonthnew:0},action ) => {


	if( action.type === GROUP ){
		return { ...state,...action.data.data }	
	}else if( action.type === GROUPCOUNT ){

		action.data.data.gttotal = action.data.data.groupTotalNum
		action.data.data.tomonthexpire = action.data.data.expireGroupNum
		action.data.data.tomonthnew = action.data.data.newGroupNum

		return { ...state,...action.data.data }	
	}else{
		return state
	}
}

const storeList = ( state = {detail:[],
							total:0,
							totalNum:0,
							gttotal:0,
							tomonthexpire:0,
							tomonthnew:0},action ) => {

	if( action.type === STORE ){
		return { ...state,...action.data.data }	
	}else if( action.type === STORECOUNT ){
		action.data.data.gttotal = action.data.data.stores
		action.data.data.tomonthexpire = action.data.data.expireStoreNum
		action.data.data.tomonthnew = action.data.data.newStoreNum

		return { ...state,...action.data.data }	
	}else{
		return state
	}
	
}

// const infoList = ( state=[],action ) => {
// 	if( action.type === INFOLIST ){
// 		state = action.data.data
// 	}
// 	return state
// }

const groupDetail = ( state={},action ) => {
	if( action.type === GROUPDETAIL ){
		state = action.data.data
	}
	if( action.type === STOREDETAIL ){
		state = action.data.data
	}
	return state
}

const alreadyOpened = ( state=[],action ) => {
	if( action.type === GROUPALREADYOPEN ){
		state = action.data.data
	}
	if( action.type === STOREALREADYOPEN ){
		state = action.data.data
	}
	return state	
}

const allMsg = ( state={ bizcount:0,
						 syscount:0,
						 msglist:[] },action ) =>{
	var newstate = {}
	if( action.type === BIZMSGCOUNT || action.type === SYSMSGCOUNT || action.type === INFOLIST ){
		newstate.bizcount = state.bizcount
		newstate.syscount = state.syscount
		newstate.msglist = state.msglist
	}
	if( action.type === BIZMSGCOUNT ){
		newstate.bizcount = action.data.data
	}

	if( action.type === SYSMSGCOUNT ){
		newstate.syscount = action.data.data
	}

	if( action.type === INFOLIST ){
		newstate.msglist = action.data.data === null ? [] : action.data.data
	}

	if( newstate.bizcount !== undefined ){
		return newstate
	}else{
		return state
	}
} 
//detail 是all ，所有的
const applyOrderList = ( state={ 
									detail:[],
									verify:{detail:[]},
									verified:{detail:[]},
									cancel:{detail:[]},
									payed:{detail:[]}
								},action ) => {
	if( action.type === APPLYORDERLIST ){
		if( action.params.applyStatus === -1 ){
			if( action.data.data.detail !== null ){
				return {...state,...action.data.data}
			}else{
				return state
			}
		
		}
		if( action.params.applyStatus === 0 ){
			let newstate = action.data.data.detail === null ?  { verify:{detail:[]} }:{ verify:action.data.data }
			return {...state,...newstate}
		}
		if( action.params.applyStatus === 3 ){
			let newstate = action.data.data.detail === null ? { verified:{detail:[]} }:{ verified:action.data.data }
			return {...state,...newstate}
		}
		if( action.params.applyStatus === 2 ){
			let newstate = action.data.data.detail === null ? { cancel:{detail:[]} }:{ cancel:action.data.data }
			return {...state,...newstate}
		}

        if( action.params.applyStatus === 4 ){
            let newstate = action.data.data.detail === null ? { payed:{detail:[]} }:{ payed:action.data.data }
            return {...state,...newstate}
        }
	}
	return state
}
const applyOrderDetail = (
							state = {
								applydetail:{storeList:[]},
								applytrack:{}
							},
							action
						  ) => {

	if( action.type === APPLYORDERDETAIL ){

		let newstate = { applydetail:action.data.data }
		return { ...state,...newstate }
	}
	if( action.type === APPLYORDERTRACK ){

		let newstate = { applytrack:action.data.data }
		return { ...state,...newstate }
	}

	return state
}
const userInfo = ( state={},action ) => {
	if( action.type === USERINFO ){
		return { ...state,...action.data.data }
	}
	return state
}
const servicerInfo = ( state={},action ) => {
	if( action.type === SERVICERINFO ){
		return { ...state,...action.data.data }
	}
	if( action.type === MODIFYSERVICER ){
		return { ...state,...action.params }
	}
	return state
}

const locationInfo = ( state={
								area:[],
								province:[],
								city:[],
								region:[]
							 },action ) => {

	if( action.type === AREA ){
		let newstate = {}
		if( state.area.length === 0 ){				
			newstate.area = action.data.data.map( (item,i)=>{
				return {
					label:item.name,
					value:item.code
				}
			} )
		}
		return { ...state,...newstate }
	}

	if( action.type === PROVINCE ){
		let newstate = {}
		if( state.province.length === 0 ){
			newstate.province = action.data.data.map( (item,i)=>{
				return {
					label:item.name,
					value:item.code
				}
			} )
		}		
		return { ...state,...newstate }	
	}

	if( action.type === CITY ){
		let newstate = {}

		newstate.city = action.data.data.map( (item,i)=>{
				return {
					label:item.name,
					value:item.code
				}
		} )
		return { ...state,...newstate }
	}

	if( action.type === REGION ){
		let newstate = {}
		newstate.region = action.data.data.map( (item,i)=>{
				return {
					label:item.name,
					value:item.code
				}
		} )
		return { ...state,...newstate }
	}

	return state
}

const products = ( state=[],action ) => {
	if( action.type === PRODUCT ){

		return action.data.data.map( ( item,i )=>{

			return {
				productTypeName:item.productTypeName,
				productList:item.productList.map(
					( item1,i1 )=>{
						return{
							icon:item1.productLogo,
							text:item1.productName,
							target:'/appFirstPage',
							id:item1.productCode
						}
					}
				)
			}
		} )

	}
	return state
}
const wxmtstoreinfo = (state = 0 ,action) =>{
	if(action.type === WXMTSETSTOREINFO){
        return {...action.data,...state}
	}
    return state
}
const rootReducer = combineReducers({
  changeCount,
  changeNav,
  loginStatus,
  applyStatus,
  productExpire,
  groupList,
  storeList,
  // infoList,
  groupDetail,
  alreadyOpened,
  allMsg,
  applyOrderList,
  applyOrderDetail,
  userInfo,
  servicerInfo,
  locationInfo,
  products,
    wxmtstoreinfo,
    changeShopCarTotal,
	tenantPushStatus,
  routing,
  wxSmartdrs,
  wxElms
})

export default rootReducer